package com.springboot.service.system;

import com.springboot.dao.SystemMenuDao;
import com.springboot.dao.UserLoginDao;
import com.springboot.entity.SystemMenuEntity;
import com.springboot.entity.UserLoginEntity;
import com.springboot.returndata.dto.AddUpdateResourceDto;
import com.springboot.returndata.vo.CommonTreeVO;
import com.springboot.returndata.vo.ResourceAuthMenuVo;
import com.springboot.returndata.vo.ResourceMenuVo;
import com.springboot.returndata.vo.ResultVo;
import com.springboot.returndata.vo.SystemMenuVo;
import com.springboot.type.SystemUserType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.util.common.TreeVoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zyj
 * @since 2023-02-22
 */
@Service
@Slf4j
public class SystemMenuService extends ServiceImpl<SystemMenuDao, SystemMenuEntity> {

    @Resource
    private SystemMenuDao systemMenuEntityDao;

    @Resource
    private UserLoginDao userLoginDao;

    public String shouyeCode = "shouye";

    /**
     * description:
     *
     * @param
     * @return: List<ResourceMenuVo>
     * @author: zyj
     * @date: 2023/2/24
     */
    public List<ResourceMenuVo> getAllResourceMenu() {

        //获取资源类型（无根节点）
        List<SystemMenuEntity> allMenuByType = systemMenuEntityDao.getAllResourceMenu();
        //转化
        SystemMenuEntity systemMenuEntity = new SystemMenuEntity();
        List<ResourceMenuVo> resourceMenuVos = systemMenuEntity.coverResourceList(allMenuByType);

        return resourceMenuVos;
    }

    /**
     * description: 根据父节点code获取所有子节点权限标志
     *
     * @param menuParentCode
     * @return: List<String>
     * @author: zyj
     * @date: 2023/3/6
     */
    public List<String> getMenuAuthorityByCode(String menuParentCode, String userId) {
        UserLoginEntity user = userLoginDao.selectById(userId);
        List<String> menuAuthorityByCode = new ArrayList<>();
        if (StringUtils.isNotEmpty(user.getUserType()) && SystemUserType.type_root.equals(user.getUserType())) {
            //超级管理员有所有权限
            menuAuthorityByCode = systemMenuEntityDao.getMenuAuthorityByCode(menuParentCode);
        } else {
            menuAuthorityByCode = systemMenuEntityDao.getMenuAuthorityByCodeUserId(menuParentCode, userId);
        }


        return menuAuthorityByCode;
    }

    /**
     * description: 根据类型获取资源
     *
     * @return: void
     * @author: zyj
     * @date: 2023/2/22
     */
    public List getAllMenuByType(String type) {

        //获取资源类型
        List<SystemMenuEntity> allMenuByType = systemMenuEntityDao.getAllMenuByType(type);
        //转化为树列表
        List<SystemMenuVo> commonTreeVOS = new ArrayList<>();
        for (SystemMenuEntity systemMenuEntity : allMenuByType) {
            SystemMenuVo commonTreeVO = new SystemMenuVo();
            BeanUtils.copyProperties(systemMenuEntity, commonTreeVO);
            commonTreeVO.setId(systemMenuEntity.getId().toString());
            commonTreeVO.setParentId(systemMenuEntity.getPid().toString());
            //地址添加参数
            String href = systemMenuEntity.getHref();
            if (StringUtils.isNoneEmpty(href)) {
                commonTreeVO.setHref(href + "?menuCode=" + systemMenuEntity.getMenuCode());
            }

            commonTreeVOS.add(commonTreeVO);
        }

        //转化为树结构
        List listTreeVo = TreeVoUtil.getListTreeVoCommon(commonTreeVOS, "0", TreeVoUtil.key_child);
        return listTreeVo;
    }

    /**
     * 方法描述 获取首页
     * @since: 1.0
     * @param
     * @return: HomeInfo
     * @author: zyj
     * @date: 2023/3/27 17:54
     */
    public ResultVo.HomeInfo shoye(){
        SystemMenuEntity authMenuByCode = systemMenuEntityDao.getAuthMenuByCode(shouyeCode);
        ResultVo resultVo = new ResultVo();
        ResultVo.HomeInfo homes = resultVo.new HomeInfo();
        homes.setHref(authMenuByCode.getHref());
        homes.setTitle(authMenuByCode.getTitle());
        return homes;
    }


    /**
     * 方法描述 保存首页
     * @since: 1.0
     * @param shouye
     * @return: void
     * @author: zyj
     * @date: 2023/3/28 9:25
     */
    public void saveShoye(Map<String, String> shouye){
        SystemMenuEntity authMenuByCode = systemMenuEntityDao.getAuthMenuByCode(shouyeCode);
        authMenuByCode.setHref(shouye.get("shouyeHref"));
        authMenuByCode.setTitle(shouye.get("shouyeTitle"));
        systemMenuEntityDao.updateById(authMenuByCode);
    }

    /**
     * description: 更新资源
     *
     * @param addUpdateResourceDto
     * @return: void
     * @author: zyj
     * @date: 2023/2/28
     */
    public void updateResourceMenu(AddUpdateResourceDto addUpdateResourceDto) throws Exception {
        SystemMenuEntity systemMenuEntity = new SystemMenuEntity(addUpdateResourceDto);
        systemMenuEntityDao.updateById(systemMenuEntity);
    }

    /**
     * description: 新增
     *
     * @param addUpdateResourceDto
     * @return: void
     * @author: zyj
     * @date: 2023/2/28
     */
    public void addResourceMenu(AddUpdateResourceDto addUpdateResourceDto) throws Exception {
        SystemMenuEntity systemMenuEntity = new SystemMenuEntity(addUpdateResourceDto);
        systemMenuEntity.setId(null);
        systemMenuEntityDao.insert(systemMenuEntity);
    }

    /**
     * description: 根据id删除
     *
     * @param id
     * @return: void
     * @author: zyj
     * @date: 2023/2/28
     */
    public void deleteResourceMenu(Integer id, String menuCode) {
        systemMenuEntityDao.deleteByCode(menuCode);
    }
}

